home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / Mesa-1.2.1 / samples / sphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-05  |  17.7 KB  |  1,017 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. /* BEP: renamed "nearest" as "nnearest" to avoid math.h collision on AIX */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <math.h>
  30. #include <stdlib.h>
  31. #include "tk.h"
  32.  
  33.  
  34. #define FALSE 0
  35. #define TRUE  1
  36. #define PI    3.14159265358979323846
  37.  
  38.  
  39. GLenum doubleBuffer, directRender;
  40. int W = 400, H = 400;
  41.  
  42. char *imageFileName = 0;
  43. TK_RGBImageRec *image;
  44.  
  45. int numComponents;
  46.  
  47. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  48. float decal[] = {GL_DECAL};
  49. float modulate[] = {GL_MODULATE};
  50. float repeat[] = {GL_REPEAT};
  51. float clamp[] = {GL_CLAMP};
  52. float nnearest[] = {GL_NEAREST};
  53. float linear[] = {GL_LINEAR};
  54. float nearest_mipmap_nearest[] = {GL_NEAREST_MIPMAP_NEAREST};
  55. float nearest_mipmap_linear[] = {GL_NEAREST_MIPMAP_LINEAR};
  56. float linear_mipmap_nearest[] = {GL_LINEAR_MIPMAP_NEAREST};
  57. float linear_mipmap_linear[] = {GL_LINEAR_MIPMAP_LINEAR};
  58. GLint sphereMap[] = {GL_SPHERE_MAP};
  59.  
  60. float xRotation = 0.0, yRotation = 0.0;
  61. float zTranslate = -4.0;
  62. GLenum autoRotate = TRUE;
  63. GLenum deepestColor = TK_GREEN;
  64. GLenum isLit = TRUE;
  65. GLenum isFogged = FALSE;
  66. float *textureEnvironment = modulate;
  67.  
  68. struct MipMap {
  69.     int width, height;
  70.     unsigned char *data;
  71. };
  72.  
  73. int cube, cage, cylinder, torus, genericObject;
  74.  
  75. float c[6][4][4][3] = {
  76.     {
  77.     {
  78.         {
  79.         1.0, 1.0, -1.0
  80.         }, 
  81.         {
  82.         0.0, 1.0, -1.0
  83.         },
  84.         {
  85.         0.0, 0.0, -1.0
  86.         },
  87.         {
  88.         1.0, 0.0, -1.0
  89.         },
  90.     },
  91.     {
  92.         {
  93.         0.0, 1.0, -1.0
  94.         },
  95.         {
  96.         -1.0, 1.0, -1.0
  97.         }, 
  98.         {
  99.         -1.0, 0.0, -1.0
  100.         }, 
  101.         {
  102.         0.0, 0.0, -1.0
  103.         },
  104.     },
  105.     {
  106.         {
  107.         0.0,  0.0, -1.0
  108.         },
  109.         {
  110.         -1.0, 0.0, -1.0
  111.         },
  112.         {
  113.         -1.0, -1.0, -1.0
  114.         },
  115.         {
  116.         0.0, -1.0, -1.0
  117.         },
  118.     },
  119.     {
  120.         {
  121.         1.0, 0.0, -1.0
  122.         },
  123.         {
  124.         0.0, 0.0, -1.0
  125.         },
  126.         {
  127.         0.0, -1.0, -1.0
  128.         },
  129.         {
  130.         1.0, -1.0, -1.0
  131.         },
  132.     },
  133.     },
  134.     {
  135.     {
  136.         {
  137.         1.0, 1.0, 1.0
  138.         },
  139.         {
  140.         1.0, 1.0, 0.0
  141.         },
  142.         {
  143.         1.0, 0.0, 0.0
  144.         },
  145.         {
  146.         1.0, 0.0, 1.0
  147.         },
  148.     },
  149.     {
  150.         {
  151.         1.0, 1.0, 0.0
  152.         },
  153.         {
  154.         1.0, 1.0, -1.0
  155.         },
  156.         {
  157.         1.0, 0.0, -1.0
  158.         },
  159.         {
  160.         1.0, 0.0, 0.0
  161.         },
  162.     },
  163.     {
  164.         {
  165.         1.0, 0.0, -1.0
  166.         },
  167.         {
  168.         1.0, -1.0, -1.0
  169.         },
  170.         {
  171.         1.0, -1.0, 0.0
  172.         },
  173.         {
  174.         1.0, 0.0, 0.0
  175.         },
  176.     },
  177.     {
  178.         {
  179.         1.0, 0.0, 0.0
  180.         },
  181.         {
  182.         1.0, -1.0, 0.0
  183.         },
  184.         {
  185.         1.0, -1.0, 1.0
  186.         },
  187.         {
  188.         1.0, 0.0, 1.0
  189.         },
  190.     },
  191.     },
  192.     {
  193.     {
  194.         {
  195.         -1.0, 1.0, 1.0
  196.         },
  197.         {
  198.         0.0, 1.0, 1.0
  199.         },
  200.         {
  201.         0.0, 0.0, 1.0
  202.         },
  203.         {
  204.         -1.0, 0.0, 1.0
  205.         },
  206.     },
  207.     {
  208.         {
  209.         0.0, 1.0, 1.0
  210.         },
  211.         {
  212.         1.0, 1.0, 1.0
  213.         },
  214.         {
  215.         1.0, 0.0, 1.0
  216.         },
  217.         {
  218.         0.0, 0.0, 1.0
  219.         },
  220.     },
  221.     {
  222.         {
  223.         1.0, 0.0, 1.0
  224.         },
  225.         {
  226.         1.0, -1.0, 1.0
  227.         },
  228.         {
  229.         0.0, -1.0, 1.0
  230.         },
  231.         {
  232.         0.0, 0.0, 1.0
  233.         },
  234.     },
  235.     {
  236.         {
  237.         0.0, -1.0, 1.0
  238.         },
  239.         {
  240.         -1.0, -1.0, 1.0
  241.         },
  242.         {
  243.         -1.0, 0.0, 1.0
  244.         },
  245.         {
  246.         0.0, 0.0, 1.0
  247.         },
  248.     },
  249.     },
  250.     {
  251.     {
  252.         {
  253.         -1.0, 1.0, -1.0
  254.         },
  255.         {
  256.         -1.0, 1.0, 0.0
  257.         },
  258.         {
  259.         -1.0, 0.0, 0.0
  260.         },
  261.         {
  262.         -1.0, 0.0, -1.0
  263.         },
  264.     }, 
  265.     {
  266.         {
  267.         -1.0, 1.0, 0.0
  268.         },
  269.         {
  270.         -1.0, 1.0, 1.0
  271.         },
  272.         {
  273.         -1.0, 0.0, 1.0
  274.         },
  275.         {
  276.         -1.0, 0.0, 0.0
  277.         },
  278.     }, 
  279.     {
  280.         {
  281.         -1.0, 0.0, 1.0
  282.         },
  283.         {
  284.         -1.0, -1.0, 1.0
  285.         },
  286.         {
  287.         -1.0, -1.0, 0.0
  288.         },
  289.         {
  290.         -1.0, 0.0, 0.0
  291.         },
  292.     }, 
  293.     {
  294.         {
  295.         -1.0, -1.0, 0.0
  296.         },
  297.         {
  298.         -1.0, -1.0, -1.0
  299.         },
  300.         {
  301.         -1.0, 0.0, -1.0
  302.         },
  303.         {
  304.         -1.0, 0.0, 0.0
  305.         },
  306.     }, 
  307.     },
  308.     {
  309.     {
  310.         {
  311.         -1.0, 1.0, 1.0
  312.         },
  313.         {
  314.         -1.0, 1.0, 0.0
  315.         },
  316.         {
  317.         0.0, 1.0, 0.0
  318.         },
  319.         {
  320.         0.0, 1.0, 1.0
  321.         },
  322.     },
  323.     {
  324.         {
  325.         -1.0, 1.0, 0.0
  326.         },
  327.         {
  328.         -1.0, 1.0, -1.0
  329.         },
  330.         {
  331.         0.0, 1.0, -1.0
  332.         },
  333.         {
  334.         0.0, 1.0, 0.0
  335.         },
  336.     },
  337.     {
  338.         {
  339.         0.0, 1.0, -1.0
  340.         },
  341.         {
  342.         1.0, 1.0, -1.0
  343.         },
  344.         {
  345.         1.0, 1.0, 0.0
  346.         },
  347.         {
  348.         0.0, 1.0, 0.0
  349.         },
  350.     },
  351.     {
  352.         {
  353.         1.0, 1.0, 0.0
  354.         },
  355.         {
  356.         1.0, 1.0, 1.0
  357.         },
  358.         {
  359.         0.0, 1.0, 1.0
  360.         },
  361.         {
  362.         0.0, 1.0, 0.0
  363.         },
  364.     },
  365.     },
  366.     {
  367.     {
  368.         {
  369.         -1.0, -1.0, -1.0
  370.         },
  371.         {
  372.         -1.0, -1.0, 0.0
  373.         },
  374.         {
  375.         0.0, -1.0, 0.0
  376.         },
  377.         {
  378.         0.0, -1.0, -1.0
  379.         },
  380.     },
  381.     {
  382.         {
  383.         -1.0, -1.0, 0.0
  384.         },
  385.         {
  386.         -1.0, -1.0, 1.0
  387.         },
  388.         {
  389.         0.0, -1.0, 1.0
  390.         },
  391.         {
  392.         0.0, -1.0, 0.0
  393.         },
  394.     },
  395.     {
  396.         {
  397.         0.0, -1.0, 1.0
  398.         },
  399.         {
  400.         1.0, -1.0, 1.0
  401.         },
  402.         {
  403.         1.0, -1.0, 0.0
  404.         },
  405.         {
  406.         0.0, -1.0, 0.0
  407.         },
  408.     },
  409.     {
  410.         {
  411.         1.0, -1.0, 0.0
  412.         },
  413.         {
  414.         1.0, -1.0, -1.0
  415.         },
  416.         {
  417.         0.0, -1.0, -1.0
  418.         },
  419.         {
  420.         0.0, -1.0, 0.0
  421.         },
  422.     },
  423.     }
  424. };
  425.  
  426. float n[6][3] = {
  427.     {
  428.     0.0, 0.0, -1.0
  429.     },
  430.     {
  431.     1.0, 0.0, 0.0
  432.     },
  433.     {
  434.     0.0, 0.0, 1.0
  435.     },
  436.     {
  437.     -1.0, 0.0, 0.0
  438.     },
  439.     {
  440.     0.0, 1.0, 0.0
  441.     },
  442.     {
  443.     0.0, -1.0, 0.0
  444.     }
  445. };
  446.  
  447. GLfloat identity[16] = {
  448.     1, 0, 0, 0,
  449.     0, 1, 0, 0,
  450.     0, 0, 1, 0,
  451.     0, 0, 0, 1,
  452. };
  453.  
  454.  
  455. void BuildCylinder(int numEdges)
  456. {
  457.     int i, top = 1.0, bottom = -1.0;
  458.     float x[100], y[100], angle; 
  459.     
  460.     for (i = 0; i <= numEdges; i++) {
  461.     angle = i * 2.0 * PI / numEdges;
  462.     x[i] = cos(angle);   /* was cosf() */
  463.     y[i] = sin(angle);   /* was sinf() */
  464.     }
  465.  
  466.     glNewList(cylinder, GL_COMPILE);
  467.     glBegin(GL_TRIANGLE_STRIP);
  468.     for (i = 0; i <= numEdges; i++) {
  469.         glNormal3f(x[i], y[i], 0.0);
  470.         glVertex3f(x[i], y[i], bottom);
  471.         glVertex3f(x[i], y[i], top);
  472.     }
  473.     glEnd();
  474.     glBegin(GL_TRIANGLE_FAN);
  475.     glNormal3f(0.0, 0.0, 1.0);
  476.     glVertex3f(0.0, 0.0, top);
  477.     for (i = 0; i <= numEdges; i++) {
  478.         glVertex3f(x[i], -y[i], top);
  479.     }
  480.     glEnd();
  481.     glBegin(GL_TRIANGLE_FAN);
  482.     glNormal3f(0.0, 0.0, -1.0);
  483.     glVertex3f(0.0, 0.0, bottom);
  484.     for (i = 0; i <= numEdges; i++) {
  485.         glVertex3f(x[i], y[i], bottom);
  486.     }
  487.     glEnd();
  488.     glEndList();
  489. }
  490.  
  491. void BuildTorus(float rc, int numc, float rt, int numt)
  492. {
  493.     int i, j, k;
  494.     double s, t;
  495.     double x, y, z;
  496.     double pi, twopi;
  497.  
  498.     pi = 3.14159265358979323846;
  499.     twopi = 2.0 * pi;
  500.  
  501.     glNewList(torus, GL_COMPILE);
  502.     for (i = 0; i < numc; i++) {
  503.     glBegin(GL_QUAD_STRIP);
  504.         for (j = 0; j <= numt; j++) {
  505.         for (k = 0; k <= 1; k++) {
  506.         s = (i + k) % numc + 0.5;
  507.         t = j % numt;
  508.  
  509.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  510.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  511.         z = sin(s*twopi/numc);
  512.         glNormal3f(x, y, z);
  513.  
  514.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  515.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  516.         z = rc * sin(s*twopi/numc);
  517.         glVertex3f(x, y, z);
  518.         }
  519.         }
  520.     glEnd();
  521.     }
  522.     glEndList();
  523. }
  524.  
  525. void BuildCage(void)
  526. {
  527.     int i, j;
  528.     float inc;
  529.     float right, left, top, bottom, front, back;
  530.  
  531.     front  = 0.0;
  532.     back   = -8.0;
  533.  
  534.     left   = -4.0;
  535.     bottom = -4.0;
  536.     right  = 4.0;
  537.     top    = 4.0; 
  538.  
  539.     inc = 2.0 * 4.0 * 0.1;
  540.  
  541.     glNewList(cage, GL_COMPILE);
  542.     for (i = 0; i < 10; i++) {
  543.  
  544.     /*
  545.     ** Back
  546.     */
  547.     glBegin(GL_LINES);
  548.         glVertex3f(left+i*inc, top,    back);
  549.         glVertex3f(left+i*inc, bottom, back);
  550.     glEnd();
  551.     glBegin(GL_LINES);
  552.         glVertex3f(right, bottom+i*inc, back);
  553.         glVertex3f(left,  bottom+i*inc, back);
  554.     glEnd();
  555.  
  556.     /*
  557.     ** Front
  558.     */
  559.     glBegin(GL_LINES);
  560.         glVertex3f(left+i*inc, top,    front);
  561.         glVertex3f(left+i*inc, bottom, front);
  562.     glEnd();
  563.     glBegin(GL_LINES);
  564.         glVertex3f(right, bottom+i*inc, front);
  565.         glVertex3f(left,  bottom+i*inc, front);
  566.     glEnd();
  567.  
  568.     /*
  569.     ** Left
  570.     */
  571.     glBegin(GL_LINES);
  572.         glVertex3f(left, bottom+i*inc, front);
  573.         glVertex3f(left, bottom+i*inc, back);
  574.     glEnd();
  575.     glBegin(GL_LINES);
  576.         glVertex3f(left, top,    back+i*inc);
  577.         glVertex3f(left, bottom, back+i*inc);
  578.     glEnd();
  579.  
  580.     /*
  581.     ** Right
  582.     */
  583.     glBegin(GL_LINES);
  584.         glVertex3f(right, top-i*inc, front);
  585.         glVertex3f(right, top-i*inc, back);
  586.     glEnd();
  587.     glBegin(GL_LINES);
  588.         glVertex3f(right, top,    back+i*inc);
  589.         glVertex3f(right, bottom, back+i*inc);
  590.     glEnd();
  591.  
  592.     /*
  593.     ** Top
  594.     */
  595.     glBegin(GL_LINES);
  596.         glVertex3f(left+i*inc, top, front);
  597.         glVertex3f(left+i*inc, top, back);
  598.     glEnd();
  599.     glBegin(GL_LINES);
  600.         glVertex3f(right, top, back+i*inc);
  601.         glVertex3f(left,  top, back+i*inc);
  602.     glEnd();
  603.  
  604.     /*
  605.     ** Bottom
  606.     */
  607.     glBegin(GL_LINES);
  608.         glVertex3f(right-i*inc, bottom, front);
  609.         glVertex3f(right-i*inc, bottom, back);
  610.     glEnd();
  611.     glBegin(GL_LINES);
  612.         glVertex3f(right, bottom, back+i*inc);
  613.         glVertex3f(left,  bottom, back+i*inc);
  614.     glEnd();
  615.     }
  616.     glEndList();
  617. }
  618.  
  619. void BuildCube(void)
  620. {
  621.     int i, j;
  622.  
  623.     glNewList(cube, GL_COMPILE);
  624.     for (i = 0; i < 6; i++) {
  625.     for (j = 0; j < 4; j++) {
  626.         glNormal3fv(n[i]); 
  627.         glBegin(GL_POLYGON);
  628.         glVertex3fv(c[i][j][0]);
  629.         glVertex3fv(c[i][j][1]);
  630.         glVertex3fv(c[i][j][2]);
  631.         glVertex3fv(c[i][j][3]);
  632.         glEnd();
  633.     }
  634.     }
  635.     glEndList();
  636. }
  637.  
  638. void BuildLists(void)
  639. {
  640.  
  641.     cube = glGenLists(1);
  642.     BuildCube();
  643.  
  644.     cage = glGenLists(2);
  645.     BuildCage();
  646.  
  647.     cylinder = glGenLists(3);
  648.     BuildCylinder(60);
  649.  
  650.     torus = glGenLists(4);
  651.     BuildTorus(0.65, 20, .85, 65);
  652.  
  653.     genericObject = torus;
  654. }
  655.  
  656. void SetDeepestColor(void)
  657. {
  658.     GLint redBits, greenBits, blueBits;
  659.  
  660.     glGetIntegerv(GL_RED_BITS, &redBits);
  661.     glGetIntegerv(GL_GREEN_BITS, &greenBits);
  662.     glGetIntegerv(GL_BLUE_BITS, &blueBits);
  663.  
  664.     deepestColor = (redBits >= greenBits) ? TK_RED : TK_GREEN;
  665.     deepestColor = (deepestColor >= blueBits) ? deepestColor : TK_BLUE; 
  666. }
  667.  
  668. void SetDefaultSettings(void)
  669. {
  670.  
  671.     magFilter = nnearest;
  672.     minFilter = nnearest;
  673.     sWrapMode = repeat;
  674.     tWrapMode = repeat;
  675.     textureEnvironment = modulate;
  676.     autoRotate = TRUE;
  677. }
  678.  
  679. unsigned char *AlphaPadImage(int bufSize, unsigned char *inData, int alpha)
  680. {
  681.     unsigned char *outData, *out_ptr, *in_ptr;
  682.     int i;
  683.  
  684.     outData = (unsigned char *) malloc(bufSize * 4);
  685.     out_ptr = outData;
  686.     in_ptr = inData;
  687.  
  688.     for (i = 0; i < bufSize; i++) {
  689.     *out_ptr++ = *in_ptr++;
  690.     *out_ptr++ = *in_ptr++;
  691.     *out_ptr++ = *in_ptr++;
  692.     *out_ptr++ = alpha;
  693.     }
  694.  
  695.     free (inData);
  696.     return outData;
  697. }
  698.  
  699. void Init(void)
  700. {
  701.     float ambient[] = {0.0, 0.0, 0.0, 1.0};
  702.     float diffuse[] = {0.0, 1.0, 0.0, 1.0};
  703.     float specular[] = {1.0, 1.0, 1.0, 1.0};
  704.     float position[] = {2.0, 2.0,  0.0, 1.0};
  705.     float fog_color[] = {0.0, 0.0, 0.0, 1.0};
  706.     float mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  707.     float mat_shininess[] = {90.0};
  708.     float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  709.     float mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  710.     float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0};
  711.     float lmodel_twoside[] = {GL_TRUE};
  712.     float lmodel_local_viewer[] = {GL_FALSE};
  713.  
  714.     SetDeepestColor();
  715.     SetDefaultSettings();
  716.  
  717.     if (numComponents == 4) {
  718.     image = tkRGBImageLoad(imageFileName);
  719.     image->data = AlphaPadImage(image->sizeX*image->sizeY,
  720.                                     image->data, 128);
  721.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  722.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  723.               image->sizeX, image->sizeY, 
  724.               GL_RGBA, GL_UNSIGNED_BYTE, image->data);
  725.     } else {
  726.     image = tkRGBImageLoad(imageFileName);
  727.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  728.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  729.               image->sizeX, image->sizeY, 
  730.               GL_RGB, GL_UNSIGNED_BYTE, image->data);
  731.     }
  732.     
  733.     glFogf(GL_FOG_DENSITY, 0.125);
  734.     glFogi(GL_FOG_MODE, GL_LINEAR);
  735.     glFogf(GL_FOG_START, 4.0);
  736.     glFogf(GL_FOG_END, 9.0);
  737.     glFogfv(GL_FOG_COLOR, fog_color);
  738.  
  739.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  740.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  741.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  742.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  743.     
  744.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  745.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  746.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  747.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  748.  
  749.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  750.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  751.     glShadeModel(GL_SMOOTH);
  752.  
  753.     glEnable(GL_LIGHTING);
  754.     glEnable(GL_LIGHT0);
  755.  
  756.     glClearColor(0.0, 0.0, 0.0, 0.0);
  757.     glViewport(0, 0, W, H);
  758.     glEnable(GL_DEPTH_TEST);
  759.  
  760.     glFrontFace(GL_CW);
  761.     glEnable(GL_CULL_FACE);
  762.     glCullFace(GL_BACK);
  763.  
  764.     glEnable(GL_TEXTURE_2D);
  765.     glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  766.     glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  767.     glEnable(GL_TEXTURE_GEN_S);
  768.     glEnable(GL_TEXTURE_GEN_T);
  769.  
  770.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  771.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  772.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  773.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  774.  
  775.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  776.  
  777.     BuildLists();
  778. }
  779.  
  780. void ReInit(void)
  781. {
  782.  
  783.     if (genericObject == torus) {
  784.     glEnable(GL_DEPTH_TEST);
  785.     } else  {
  786.     glDisable(GL_DEPTH_TEST);
  787.     }
  788.     if (isFogged) {
  789.     textureEnvironment = modulate;
  790.     }
  791.  
  792.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  793.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  794.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  795. }
  796.  
  797. void Draw(void)
  798. {
  799.  
  800.     glMatrixMode(GL_PROJECTION);
  801.     glLoadIdentity();
  802.     glFrustum(-0.2, 0.2, -0.2, 0.2, 0.15, 9.0);
  803.     glMatrixMode(GL_MODELVIEW);
  804.  
  805.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  806.     if (isFogged) {
  807.     glEnable(GL_FOG);
  808.     glColor3fv(tkRGBMap[deepestColor]);
  809.     } else {
  810.     glColor3fv(tkRGBMap[TK_WHITE]);
  811.     }
  812.     glDisable(GL_LIGHTING);
  813.     glDisable(GL_LIGHT0);
  814.     glDisable(GL_TEXTURE_2D);
  815.     glCallList(cage);
  816.  
  817.     glPushMatrix();
  818.     glTranslatef(0.0, 0.0, zTranslate);
  819.     glRotatef(xRotation, 1, 0, 0);
  820.     glRotatef(yRotation, 0, 1, 0);
  821.  
  822.     if (isLit == TRUE) {
  823.     glEnable(GL_LIGHTING);
  824.     glEnable(GL_LIGHT0);
  825.     }
  826.  
  827.     glEnable(GL_TEXTURE_2D);
  828.     if (isFogged) {
  829.     glDisable(GL_FOG);
  830.     }
  831.     glPolygonMode(GL_FRONT, GL_FILL);
  832.     glColor3fv(tkRGBMap[deepestColor]);
  833.     glCallList(genericObject);
  834.  
  835.     glPopMatrix();
  836.     glFlush();
  837.  
  838.     if (autoRotate) {
  839.     xRotation += .75;
  840.     yRotation += .375;
  841.     } 
  842.     tkSwapBuffers();
  843. }
  844.  
  845. void Reshape(int width, int height)
  846. {
  847.  
  848.     W = width;
  849.     H = height;
  850.     ReInit();
  851. }
  852.  
  853. GLenum Key(int key, GLenum mask)
  854. {
  855.  
  856.     switch (key) {
  857.       case TK_ESCAPE:
  858.     free(image->data);
  859.     tkQuit();
  860.  
  861.       case TK_LEFT:
  862.     yRotation -= 0.5;
  863.     autoRotate = FALSE;
  864.     ReInit();
  865.     break;
  866.       case TK_RIGHT:
  867.     yRotation += 0.5;
  868.     autoRotate = FALSE;
  869.     ReInit();
  870.     break;
  871.       case TK_UP:
  872.     xRotation -= 0.5;
  873.     autoRotate = FALSE;
  874.     ReInit();
  875.     break;
  876.       case TK_DOWN:
  877.     xRotation += 0.5;
  878.     autoRotate = FALSE;
  879.     ReInit();
  880.     break;
  881.       case TK_a:
  882.     autoRotate = !autoRotate;
  883.     ReInit();
  884.     break;
  885.       case TK_c:
  886.     genericObject = (genericObject == cube) ? cylinder : cube;
  887.     ReInit();
  888.     break;
  889.       case TK_d:
  890.     textureEnvironment = decal;
  891.     ReInit();
  892.     break;
  893.       case TK_m:
  894.     textureEnvironment = modulate;
  895.     ReInit();
  896.     break;
  897.       case TK_l:
  898.     isLit = !isLit;
  899.     ReInit();
  900.     break;
  901.       case TK_f:
  902.     isFogged = !isFogged;
  903.     ReInit();
  904.     break;
  905.       case TK_t:
  906.     genericObject = torus;
  907.     ReInit();
  908.     break;
  909.       case TK_0:
  910.     magFilter = nnearest;
  911.     ReInit();
  912.     break;
  913.       case TK_1:
  914.     magFilter = linear;
  915.     ReInit();
  916.     break;
  917.       case TK_2:
  918.     minFilter = nnearest;
  919.     ReInit();
  920.     break;
  921.       case TK_3:
  922.     minFilter = linear;
  923.     ReInit();
  924.     break;
  925.       case TK_4:
  926.     minFilter = nearest_mipmap_nearest;
  927.     ReInit();
  928.     break;
  929.       case TK_5:
  930.     minFilter = nearest_mipmap_linear;
  931.     ReInit();
  932.     break;
  933.       case TK_6:
  934.     minFilter = linear_mipmap_nearest;
  935.     ReInit();
  936.     break;
  937.       case TK_7:
  938.     minFilter = linear_mipmap_linear;
  939.     ReInit();
  940.     break;
  941.  
  942.       default:
  943.     return GL_FALSE;
  944.     }
  945.     return GL_TRUE;
  946. }
  947.  
  948. GLenum Args(int argc, char **argv)
  949. {
  950.     GLint i;
  951.  
  952.     doubleBuffer = GL_FALSE;
  953.     directRender = GL_TRUE;
  954.     numComponents = 4;
  955.  
  956.     for (i = 1; i < argc; i++) {
  957.     if (strcmp(argv[i], "-sb") == 0) {
  958.         doubleBuffer = GL_FALSE;
  959.     } else if (strcmp(argv[i], "-db") == 0) {
  960.         doubleBuffer = GL_TRUE;
  961.     } else if (strcmp(argv[i], "-dr") == 0) {
  962.         directRender = GL_TRUE;
  963.     } else if (strcmp(argv[i], "-ir") == 0) {
  964.         directRender = GL_FALSE;
  965.     } else if (strcmp(argv[i], "-f") == 0) {
  966.         if (i+1 >= argc || argv[i+1][0] == '-') {
  967.         printf("-f (No file name).\n");
  968.         return GL_FALSE;
  969.         } else {
  970.         imageFileName = argv[++i];
  971.         }
  972.     } else if (strcmp(argv[i], "-4") == 0) {
  973.         numComponents = 4;
  974.     } else if (strcmp(argv[i], "-3") == 0) {
  975.         numComponents = 3;
  976.     } else {
  977.         printf("%s (Bad option).\n", argv[i]);
  978.         return GL_FALSE;
  979.     }
  980.     }
  981.     return GL_TRUE;
  982. }
  983.  
  984. void main(int argc, char **argv)
  985. {
  986.     GLenum type;
  987.  
  988.     if (Args(argc, argv) == GL_FALSE) {
  989.     tkQuit();
  990.     }
  991.  
  992.     if (imageFileName == 0) {
  993.     printf("No image file.\n");
  994.     tkQuit();
  995.     }
  996.  
  997.     tkInitPosition(0, 0, W, H);
  998.  
  999.     type = TK_RGB;
  1000.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  1001.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  1002.     tkInitDisplayMode(type);
  1003.  
  1004.     if (tkInitWindow("Texture Test") == GL_FALSE) {
  1005.         tkQuit();
  1006.     }
  1007.  
  1008.     Init();
  1009.  
  1010.     tkExposeFunc(Reshape);
  1011.     tkReshapeFunc(Reshape);
  1012.     tkKeyDownFunc(Key);
  1013.     tkIdleFunc(Draw);
  1014.  
  1015.     tkExec();
  1016. }
  1017.